import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    saveCreditEvaluationForm, //保存
    getCreditEvaluationFormByfId, //详情

} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "projectId",
        "status",
        "statusNum",
        "dataFid",
        "currentRow",
        "projectName",
        "fCompanyid"
    ],
    data() {
        return {
            visible: "",
            title: "信用评价清单",
            isLock: false,
            btnStatus: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            currntData: null,
            dataInfo: null,
            startDates: null,
            formData: {
                fId: "", //信息的fid
                userId: "",
                userName: "",
                fCompanyid: "", //所属单位ID
                fCompanyname: "", //所属单位名称
                fEvaluationcontent: "", //评价内容
                fEvaluationproject: "", //评价项目
                fBehaviorcode: "", //失信行为代码
                fCreditlevel: "", //信用等级或扣分标准
                fDishonestbehaviordesc: "", //失信行为
                fNote: "" //备注
            },
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentStatus: null,
        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        projectId: {
            handler(newVal) {
                if (newVal) {
                    console.log(newVal);
                }
            },
            immediate: true,
        },
        status: {
            handler(newVal) {
                if (newVal) {
                    this.btnStatus = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        dataFid: {
            handler(newVal) {
                if (newVal) {
                    this.currntData = newVal;
                    if (this.statusNum != 1) {
                        this.formData = this.$options.data.call(this).formData;
                        this.getDetail({
                            fId: newVal,
                        });
                    }
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentStatus = newVal;
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    // this.currentStatus = newVal;
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
        if (this.statusNum == 1) {
            this.btnShow = true
            this.getDetail({
                fId: ""
            });
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[2].disabled = true;

        } else if (this.statusNum == 2) { //编辑
            //编辑
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[2].disabled = true;
            this.btnShow = true
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        // fCcpeopleData计划传阅人
        selectChange(data, a, b) {
            if (b == "fWeekdates") {
                this.getWeeks(this.formData.fWeekdates)
            }
        },
        edit(status) {
            if (status == 1) {
                this.formData = this.$options.data.call(this).formData;
            } else if (status == 2) {
                this.formData = this.$options.data.call(this).formData;
            } else if (status == 4) {
                this.monthExamineData = this.$options.data.call(this).monthExamineData;
            }
        },
        // 详情
        getDetail(data1) {
            getCreditEvaluationFormByfId(data1).then((res) => {
                if (res.errcode == 0) {

                    if (this.statusNum == 1) {
                        this.btnShow = true;
                    } else if (this.statusNum == 2) {
                        this.btnShow = true;
                    } else if (this.statusNum == 7) {
                        this.btnShow = false;
                    }
                    var data = res.data.creditEvalutionform;
                    if (data1.fId != '') {
                        this.formData = data
                        this.formDatafileList = data.fFileurllist
                        this.formData.recordfiles = []
                    } else {
                        this.formData = this.$options.data.call(this).formData;
                        this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
                        this.formData.fBehaviorcode = data.fBehaviorcode
                        this.formData.fCompanyname = this.userInfo.companyFullName; //当前登录人标段名称
                        this.formData.fCompanyid = this.userInfo.companyId; //当前登录人标段ID
                        this.formData.userId = this.userInfo.userId; //当前登录人id
                        this.formData.userName = this.userInfo.realName; //当前登录人名称
                    }
                }
            });
        },
        getDaysBetween(date1, date2) {
            const ONE_DAY = 1000 * 60 * 60 * 24; // 一天的毫秒数
            const date1Time = date1.getTime(); // 获取时间戳
            const date2Time = date2.getTime();
            const difference = Math.abs(date1Time - date2Time); // 获取时间差
            return Math.round(difference / ONE_DAY); // 两个日期之间的天数
        },
        yj_saveForm() {
            console.log(888, this.statusNum);
            if (this.statusNum == 1) { //新增
                if (this.$refs.publicForm.submitForm()) {
                    this.saveForm(this.formData);
                } else {
                    console.log("error");
                }
            } else if (this.statusNum == 2) { //编辑
                if (this.$refs.publicForm.submitForm()) {
                    this.saveForm(this.formData);
                } else {
                    console.log("error");
                }
            }
        },
        // 保存
        saveForm(data) {

            this.isLock = true
            saveCreditEvaluationForm(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });

        },
    },
};